<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<link rel="icon" type="image/png" href="img/favicon.ico">
		<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
		<title>指纹单元</title>
		<link rel="stylesheet" href="css/style.min.css" />
	</head>
	<body>
		<header id="header" class="header">
			<div class="logo">
				<img src="img/logo.png" alt="logo" style="height: 62px;">
			</div>
			<div class="nav">
				<ul class="header-list">
					<li>
						<a class="active" href="#">指纹库</a>
					</li>
					<li>
						<a href="config.html">系统设置</a>
					</li>
					<li>
						<a href="collect.html">指纹录入</a>
					</li>
					
					<li id="fingerid" style="margin-left: 100px;color: #005CBF;"></li>
				</ul>
			</div>
		</header>
		<div class="content">
			<table cellpadding="0" cellspacing="0">
				<thead>
					<tr>
						<th>工号</th>
						<th>姓名</th>
						<th>性别</th>
						<th>指纹</th>
						<th>#</th>
					</tr>
				</thead>
				<tbody id="emplist">
					
				</tbody>
			</table>
			<button type="button" class="btn btn-success btn-lg btn-block" onclick="javascript:addemp();">添加员工</button>
			<button type="button" class="btn btn-success btn-lg btn-block" onclick="javascript:empty();">清空指纹</button>
		</div>
	</body>
	<script type="text/javascript" src="js/app.min.js"></script>
	<script type="application/javascript">
		window.onload = function() {
			ajax({
				url: 'onload.cgi',
				data: {
					method: 'listemp'
				},
				method: 'POST',
				success: function(rsp) {
					var empbody = document.getElementById('emplist');
					var obj = JSON.parse(rsp);
					var html = "";
					if(obj.res == '1')
						return;
					obj.forEach(function(emp) {
						html += "<tr id='" + emp.id + "'>";
						html += "<td>" + emp.code + "</td>";
						html += "<td>" + emp.name + "</td>";
						html += "<td>";
						if (emp.sex == '1') {
							html += "男"
						} else {
							html += "女"
						}
						html += "</td>";
						html += "<td>";
						if (emp.hrcode == '1') {
							html += "<span class='badge badge-success'>已采集</span>"
						} else {
							html += "<span class='badge badge-warning'>未采集</span>"
						}
						html += "</td>";
						html += "<td>";
						html += "<a class='badge badge-primary' href='javascript: void(0);' onclick=\"javascript:collect('" + emp.id + "');\">采集</a>";
						html += "<a class='badge badge-secondary' style='background-color: green;'>编辑</a>";
						html += "<a class='badge badge-danger' href='javascript: void(0);' onclick=\"javascript:delemp('" + emp.id + "');\">删除</a>";
						html += "</td>";
						html += "</tr>"
					});
					empbody.innerHTML = html
				},
				error: function(rsp) {
					console.log(rsp);
				}
			})
		}
		function delemp(ecod) {
			ajax({
				url: 'delemp.cgi',
				data: {
					id: ecod
				},
				method: 'POST',
				success: function(rsp) {
					var obj = JSON.parse(rsp);
					if(obj.res == '1')
					{
						var d = document.getElementById(ecod);
						d.remove(d.selectedIndex)
					}
				},
				error: function(rsp) {
					console.log(rsp);
				}
			})
		}
		function collect(id) {
			ajax({
				url: 'collect.cgi',
				data: {
					id: id
				},
				method: 'POST',
				success: function(rsp) {
					var obj = JSON.parse(rsp);
					if (obj.res == '1') {
						window.location.href = 'collect.html';
					}
				},
				error: function(rsp) {
					console.log(rsp);
				}
			})
		}
		function empty() {
			ajax({
				url: 'empty.cgi',
				data: {
					method: 'empty'
				},
				method: 'POST',
				success: function(rsp) {
					var obj = JSON.parse(rsp);
					if (obj.res == '1') {
						window.location.href = 'index.html';
					}
				},
				error: function(rsp) {
					console.log(rsp);
				}
			})
		}
		function addemp(){
			window.location.href='empedit.html';
		}
		readFingerPrint();
		function readFingerPrint(){ 			 
			ajax({
			 	url: 'readFgPr.cgi',
			 	method: 'POST',
				data: {
					method: 'readFgPr'
				},
			 	success: function(rsp) {
			 		var obj = JSON.parse(rsp);
			 		if (obj.code != undefined) {
			 			document.getElementById("fingerid").textContent = obj.code;
			 		}
			 	},
			 	error: function(rsp) {
			 		console.log(rsp);
			 	}
			})
			setTimeout('readFingerPrint()',1000); 
		}
	</script>
</html>
